package copyengine.ui.component.tabbar
{
    import copyengine.log.CELog;
    import copyengine.ui.component.CEGUIComponent;
    import copyengine.ui.component.button.CESelectableButton;

    import starling.display.DisplayObject;

    public class CETabbar extends CEGUIComponent
    {
        private var mOnTabChangeCallback:Function;
        private var mCurrentTab:String;
        private var mSubSelectableBtnVector:Vector.<CESelectableButton>;

        public function CETabbar(_container:DisplayObject, _subSelectableBtnVector:Vector.<CESelectableButton>, _defaultTab:String)
        {
            super(_container);
            mSubSelectableBtnVector = _subSelectableBtnVector;
            for each(var eachBtn:CESelectableButton in mSubSelectableBtnVector) {
                eachBtn.isClickToSelect = false;
                eachBtn.isSelected = eachBtn.name == _defaultTab;
                eachBtn.onClickCallBack = subBtnOnClick;
            }
        }

        //==============================//
        //==Public Interface  Function
        //==============================//

        public function set onTabChangeCallback(_callback:Function):void {mOnTabChangeCallback = _callback;}

        /**
         * 返回当前的Tab
         */
        public function get currentTab():String
        { return mCurrentTab; }

        public function set currentTab(_newTab:String):void
        {
            if (mCurrentTab != _newTab) {
                mCurrentTab = _newTab;
                for each (var btn:CESelectableButton in mSubSelectableBtnVector) {
                    btn.isSelected = btn.name == mCurrentTab;
                }
            }
        }

        /**
         *找到当前Tabbar下面的某个Button按钮
         */
        public function getTabButtonByName(_tabName:String):CESelectableButton
        {
            for each (var btn:CESelectableButton in mSubSelectableBtnVector) {
                if (btn.name == _tabName) {
                    return btn;
                }
            }
            return null;
        }

        //========================//
        //==Private  Function
        //=========================//

        private function subBtnOnClick(_targetBtn:CESelectableButton):void
        {
            var clickedBtnName:String = _targetBtn.container.name;
            if (mCurrentTab != clickedBtnName) {
                mCurrentTab = clickedBtnName;
                for each (var btn:CESelectableButton in mSubSelectableBtnVector) {
                    btn.isSelected = btn.name == clickedBtnName;
                }
                tryToCallBack(mCurrentTab);
            }
        }

        private function tryToCallBack(_currentTab:String):void
        {
            if (mOnTabChangeCallback != null) {
                if (mOnTabChangeCallback.length == 1) {
                    mOnTabChangeCallback(_currentTab);
                }
                else {
                    CELog.err("CESTabbar callback argument is wrong should be 1");
                }
            }
        }

    }
}
